/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gr.anagnosg.general.utils.text;

import gr.anagnosg.general.utils.StringUtils;
import gr.anagnosg.general.utils.diff_match_patch;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author anagnosg
 */
public class TextCompare {
    String s1= "";
    String s2 = "";
    diff_match_patch df = null;
    LinkedList<diff_match_patch.Diff> diffList = null;
    WordList wl1= null;
    WordList wl2= null;
    int wordCount1 =0;
    int wordCount2 =0;
    int charCount1 = 0 ;
    int charCount2 = 0 ;
    int compareResult = 0;    
    
    List charList1 =new ArrayList ();
    List charList2 =new ArrayList ();
    List charListDiffs = new ArrayList();

    public List getCharList1() {
        return charList1;
    }

    public void setCharList1(List charList1) {
        this.charList1 = charList1;
    }

    public List getCharList2() {
        return charList2;
    }

    public void setCharList2(List charList2) {
        this.charList2 = charList2;
    }

    public List getCharListDiffs() {
        return charListDiffs;
    }

    public void setCharListDiffs(List charListDiffs) {
        this.charListDiffs = charListDiffs;
    }
    
    
    
    public String getS1() {
        return s1;
    }

    public void setS1(String s1) {
        this.s1 = s1;
    }

    public String getS2() {
        return s2;
    }

    public void setS2(String s2) {
        this.s2 = s2;
    }

    public diff_match_patch getDf() {
        return df;
    }

    public void setDf(diff_match_patch df) {
        this.df = df;
    }

    public LinkedList<diff_match_patch.Diff> getDiffList() {
        return diffList;
    }

    public void setDiffList(LinkedList<diff_match_patch.Diff> diffList) {
        this.diffList = diffList;
    }

    public WordList getWl1() {
        return wl1;
    }

    public void setWl1(WordList wl1) {
        this.wl1 = wl1;
    }

    public WordList getWl2() {
        return wl2;
    }

    public void setWl2(WordList wl2) {
        this.wl2 = wl2;
    }

    public int getWordCount1() {
        return wordCount1;
    }

    public void setWordCount1(int wordCount1) {
        this.wordCount1 = wordCount1;
    }

    public int getWordCount2() {
        return wordCount2;
    }

    public void setWordCount2(int wordCount2) {
        this.wordCount2 = wordCount2;
    }

    public int getCharCount1() {
        return charCount1;
    }

    public void setCharCount1(int charCount1) {
        this.charCount1 = charCount1;
    }

    public int getCharCount2() {
        return charCount2;
    }

    public void setCharCount2(int charCount2) {
        this.charCount2 = charCount2;
    }

    public int getCompareResult() {
        return compareResult;
    }

    public void setCompareResult(int compareResult) {
        this.compareResult = compareResult;
    }
    
    
    
    
    public void compare(String s1,String s2){
    this.s1=s1;this.s2=s2;
    
    List wordsList1 =  wordsList1 = StringUtils.splitText2Words(s1);
    wl1= new WordList();
    wl1.setWords((ArrayList)wordsList1);
    
    List wordsList2 = StringUtils.splitText2Words(s2);
    wl2= new WordList();
    wl2.setWords((ArrayList)wordsList2);
    
    
    
    wordCount1 = wl1.wordCount();
    charCount1 = wl1.charCount();
       
       
    wordCount2 = wl2.wordCount();
    charCount2 = wl2.charCount();
    
    
    //List chars1 =wl1.generateCharList();
    //List chars2 =wl2.generateCharList();
    
    //int resultInt = 0;    
    df = new diff_match_patch();
    //s1=StringUtils.trimSpaces(s1);
    //s2=StringUtils.trimSpaces(s2);
    diffList =df.diff_main(s1, s2);
   /*if(diffList.size()>0&& !diffList.get(0).text.matches("(\\w+)")){
        diffList.remove(0);
   }*/
    System.out.println("list size"+diffList.size());
    /*for(int i=0; i < diffList.size() ; i++){
        diff_match_patch.Diff dT = diffList.get(i);
        if(dT.text.equals("\n") && (dT.operation+"").equals("DELETE") ){
            diffList.remove();
        }
        else if((dT.operation+"").equals("DELETE")){
            String textT = dT.text;
            //System.out.println("("+textT+")"+",length:"+textT.length());
            //StringUtils.printStringChars(textT);
            //System.out.println(i+","+dT.text+","+dT.operation);  
            //diff_match_patch.Diff dTIn = list.get(i+1);
           // System.out.println(i+","+dT.text+","+dT.operation+","+dTIn.text+", "+dTIn.operation);  
        }
    }*/
    //System.out.println("list size after"+diffList.size());
    /*for(int i=0; i < diffList.size() ; i++){
        //diff_match_patch.Diff dT = diffList.get(i);
        //System.out.println(i+","+dT.text+","+dT.operation);    
    }*/
    //String source = FileUtils.readTextFile("")
    


   System.out.println("w1:"+wordCount1+",c1:"+charCount1+",w2:"+wordCount2+",c2:"+charCount2+"diffList.size()"+diffList.size());
   diff_match_patch.Diff dT = diffList.get(0); 
   if(diffList.size()==1 && dT.operation==dT.operation.EQUAL && this.charCount1== this.charCount2 && this.wordCount1==this.wordCount2 ){
       this.compareResult = 0;//idia ola
   }
   else if(diffList.size()>1 && this.charCount1== this.charCount2 && this.wordCount1==this.wordCount2){
       this.compareResult = 1;//idia ola alla oxi oi xarakthres.
   }
   else if(this.charCount1== this.charCount2  && this.wordCount1>this.wordCount2 ){
       this.compareResult = -2;//idioi xarakthres perissoteres lekseis? Mporei na isueis? Me ta kena. 
   }
   else if(this.charCount1== this.charCount2  && this.wordCount1<this.wordCount2 ){
       this.compareResult = 2;
   }
   else if(this.charCount1> this.charCount2  && this.wordCount1==this.wordCount2 ){
       this.compareResult = -3; //idies lekseis diaforetikoi xarakthres? 
   }
   else if(this.charCount1< this.charCount2  && this.wordCount1==this.wordCount2 ){
       this.compareResult = 3;
   }
   else if(this.charCount1!= this.charCount2  && this.wordCount1!=this.wordCount2 ){
       this.compareResult = 4; // oute xarakthres oute lekseis idies. xaraj
   }
       //return resultInt ;     
   System.out.println("compareResult"+this.compareResult);
    
   }
    
    
     public void findCharDiff(){//equal chars counts.
        //this.charListDiffs
        this.wl1.generateCharList();
        this.charList1= wl1.getCharsList();
        this.wl2.generateCharList();
        this.charList2 = wl2.getCharsList();
        for(int i=0 ; i<this.charList2.size(); i++){
            String cT1 = (String)this.charList1.get(i);
            String cT2 =(String)this.charList2.get(i); 
            //ystem.out.println("cT1"+cT1+",cT2"+cT2);
            if(!cT1.equals(cT2)){
                Word wT = new Word();
                wT.text=cT2;
                wT.oldText=cT1;
                wT.startIndex=i;
                this.charListDiffs.add(wT);
            }
        }
   
   }
   
   public List findWordsDiff(boolean onlyLength){
       List output = new ArrayList();
       /*for(int i=0; i<this.wl1.words.size();i++){
           Word w1 = (Word)this.wl1.words.get(i);
           Word w2 = (Word)this.wl2.words.get(i);
           if(!w1.getText().equals(w2.getText())){
               if(onlyLength){
                   if(w1.getText().length()!=w2.getText().length()){
                       Word wDiff = new Word();
                       wDiff.setText(w2.getText());
                       wDiff.setOldText(w1.getText());
                       output.add(wDiff);
                       System.out.println("w1:"+w1.getText()+" w2:"+w2.getText());
                   }
               }
               else{
                       Word wDiff = new Word();
                       wDiff.setText(s1);
                       output.add(wDiff);
               }
           }
       }*/
       int i=0;
       int startNew=0;int endNew=-1;
       int startOld=0;int endOld=-1;
       while(i <diffList.size()){
           diff_match_patch.Diff dT = diffList.get(i); 
           //dT.operation==dT.operation.EQUAL
           if((dT.operation+"").equals("EQUAL")){
               startNew=endNew + 1;
               endNew=startNew+dT.text.length();
               startOld=endOld+1;
               endOld=startOld+dT.text.length();
               i++;
               continue;
            }
           else if((dT.operation+"").equals("DELETE")){
               String oldText =  dT.text;
               if(i+1<diffList.size()){
                    diff_match_patch.Diff dTNext = diffList.get(i+1); 
                    if((dTNext.operation+"").equals("EQUAL")){
                        Word wNew = new Word();
                        wNew.setText("");
                        wNew.setOldText(oldText);
                        wNew.setType(Word.DELETE);//
                        startOld+=dT.text.length();
                        endOld+=dT.text.length()-1;
                        wNew.setStartIndex(startNew);
                        wNew.setEndIndex(endNew);
                        wNew.setStartIndexOld(startOld);
                        wNew.setEndIndexOld(endOld);
                        wNew.printInfo();
                        
                        
                        
                        output.add(wNew);
                        
                    }
                    else if((dTNext.operation+"").equals("INSERT")){
                        
                        String text = dTNext.text;
                        Word wNew = new Word();
                        wNew.setText(text);
                        wNew.setOldText(oldText);
                        wNew.setType(Word.REPLACE);//
                        
                        startOld=endOld+1;
                        endOld=startOld+dT.text.length();
                        startNew=endNew+1;
                        endNew=startNew+dTNext.text.length();
                        wNew.setStartIndex(startNew);
                        wNew.setEndIndex(endNew);
                        wNew.setStartIndexOld(startOld);
                        wNew.setEndIndexOld(endOld);
                        if(!onlyLength || text.length()!=oldText.length()){
                            output.add(wNew);
                            wNew.printInfo();
                        }
                        
                    }
                    
                    i+=2;
                }
               else{
                   
                    Word wNew = new Word();
                    wNew.setText("");
                    wNew.setOldText(oldText);
                    wNew.setType(Word.DELETE);//
                    startOld=endOld+1;
                    endOld=startOld+dT.text.length();
                    wNew.setStartIndex(startNew);
                    wNew.setEndIndex(endNew);
                    wNew.setStartIndexOld(startOld);
                    wNew.setEndIndexOld(endOld);
                    output.add(wNew);
                    wNew.printInfo();
                    
                    i++;    
               }
               
           }
           else{//insert
               
                String text = dT.text;
                if(text.length()>0){
                    Word wNew = new Word();
                    wNew.setText(text);
                    wNew.setOldText("");
                    wNew.setType(Word.INSERT);//
                    startNew=endNew+1;
                    endNew=startNew +dT.text.length();
                    wNew.setStartIndex(startNew);
                    wNew.setEndIndex(endNew);
                    wNew.setStartIndexOld(startOld);
                    wNew.setEndIndexOld(endOld);
                    output.add(wNew);
                    wNew.printInfo();
                }
                i++;
           }
           
       }
       
       return output;
   
   }
}
